home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14955 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  75 lines

  1. Path: Inter.NL.net!usenet
  2. From: Auke.Reitsma@net.HCC.nl (Auke Reitsma)
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.c++.leda
  4. Subject: Re: Common var Pbs
  5. Date: Tue, 02 Apr 1996 20:16:07 GMT
  6. Organization: Inter.NL.net, The Internet Provider in The Netherlands.
  7. Message-ID: <4js1un$568@altrade.nijmegen.inter.nl.net>
  8. References: <3160642C.7606@green-uhp.u-nancy.fr>
  9. Reply-To: Auke.Reitsma@net.HCC.nl
  10. NNTP-Posting-Host: rt99-20.rotterdam.nl.net
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. Lotfi BAGHLI <baghli@green-uhp.u-nancy.fr> wrote:
  14.  
  15. > I have problems when linking 3 cpp files which use common 
  16. > variables and constants
  17. > in a BC++4.5 / OWL 2.5 application :
  18.  
  19. Even though your stuff is C++, it is a common problem in C too.
  20. I cleaned the following quotes a bit ...
  21.  
  22. > A:
  23. > ------    A.H
  24. >     --------
  25. >       enum  Jour { Lundi, Mardi, Mercredi, ... , Dimanche };
  26.  
  27. OK.
  28.  
  29. >         HBITMAP JourImage[Dimanche+1][4];
  30.  
  31. replace this by:
  32.  
  33.          extern HBITMAP JourImage[Dimanche+1][4];
  34.  
  35. >         const int Periode[Dimanche+1]={    4, 2, 0, 4, 4, 4, 0};
  36.  
  37. repalce this by:
  38.  
  39.          extern const int Periode[Dimanche+1];
  40.  
  41. >         const char * JourName[Dimanche+1]= { "Lundi", "Mardi", ..., "Dimanche" };
  42.  
  43. replace this by:
  44.  
  45.         extern const char * JourName[Dimanche+1];
  46.  
  47. >         class TJour  {...};
  48. >         typedef TIArrayAsVector <TJour>  TJourCollection;
  49.  
  50. >     A.CPP
  51. >     --------
  52. >       #include "A.H"
  53. >         methods of TJour ...
  54.  
  55. Add here:
  56.  
  57.         HBITMAP JourImage[Dimanche+1][4];
  58.          const int Periode[Dimanche+1]={    4, 2, 0, 4, 4, 4, 0};
  59.          const char * JourName[Dimanche+1]= { "Lundi","Mardi", ...,"Dimanche"};
  60.  
  61.  
  62. > The linking generates errors :
  63. > _JourImage defined in module A.CPP is duplicated in module B.CPP
  64.  
  65. The problem is that you were reserving space for variables, which is
  66. something you should only do in .c(pp) files, and NEVER in .h(pp) files.
  67. The 'extern' in the .h prevents the problem.
  68.  
  69. I.e.: use  extern XXX in .h files and XXX = some_value in .c files. With
  70. XXX the same thing for both.
  71.  
  72. Greetings from Delft, The Netherlands,
  73. Auke Reitsma.
  74.  
  75.